package com.bw.controller;

import com.bw.config.HbaseProperties;
import com.bw.config.HbasePropertiesConfig;
import com.bw.domain.TraceUserCrops;
import com.bw.service.HbaseService;
import com.bw.service.TraceUserCropsService;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Increment;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("farmers")
public class HbaseController {
    
    @Autowired
    TraceUserCropsService traceUserCropsService;
    @Autowired
    private HbaseService hbaseService;
    @Autowired
    HbaseProperties hbaseProperties;
    @Autowired
    HbasePropertiesConfig hbasePropertiesConfig;
    
    @RequestMapping("created")
    public void createTable(){
        try {
            //创建表
            hbaseService.createTable("leix","info");
            System.out.println("创建成功");
        }catch (Exception exception){
            System.out.println("创建失败");
            exception.printStackTrace();
        }
    }

    @RequestMapping("adds")
    public Map<String,Object> adds(@RequestBody TraceUserCrops trace){
        HashMap<String, Object> map = new HashMap<>();
        HashMap<String, String> maps = new HashMap<>();
        createTable();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String times = format.format(new Date());
        maps.put("农产品id",trace.getCropsId());
        maps.put("农产品名称",trace.getUsername());
        maps.put("添加用户",trace.getCropsName());
        maps.put("添加时间",times);
        String rowKey = String.valueOf(getRowKey());
        try {
            //存到hbase
            hbaseService.putData("leix","info",rowKey,maps);
            System.out.println("存入成功");
        }catch (Exception exception){
            System.out.println("存入失败");
            exception.printStackTrace();
        }
        list();
        map.put("code",200);
        map.put("msg","HBASE");
        return map;
    }
    
    
    @RequestMapping("lists")
    public Map<String,Object> list() {
        HashMap<String, Object> map = new HashMap<>();
        try {
            hbaseService.getData("leix");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return map;
    }
    
    public Integer getRowKey(){
        Integer rkey=0;
        //
        try {
            //创建连接到hbase数据库的连接对象
            Connection connection = hbasePropertiesConfig.getConnection();
            //获取指定hbase表对象
            Table table = connection.getTable(TableName.valueOf("track"));
            //创建增量操作对象并设置行键
            //行健没有数据类型，总是视为字节数组byte[]
            //利用 Bytes.toBytes(rowKey) 方法将行键转换为字节数组，并将其作为参数创建一个增量操作对象
            Increment increment = new Increment(Bytes.toBytes("rowKey"));
            //使用 addColumn 方法指定要增加的列族、列和增量值
            increment.addColumn(Bytes.toBytes("info"), Bytes.toBytes("counter"), 1);
            //获取结果
            Result result1 = table.increment(increment);
            //使用 getValue 方法从 Result 对象中获取增量操作后的值，并将其转换为 long 类型
            rkey = Bytes.toInt(result1.getValue(Bytes.toBytes("info"), Bytes.toBytes("counter")));
            System.out.println("============" + rkey);
        }catch (Exception e){
            e.printStackTrace();
        }
        
        return rkey;
    }
    
    
    public static void main(String[] args) {
        System.out.println("test");
    }

}
